package com.chixing.ih.module.programmer.service.Impl;

import com.chixing.ih.module.programmer.mapper.AttentionMapper;
import com.chixing.ih.module.programmer.mapper.ImageMapper;
import com.chixing.ih.module.programmer.mapper.ProgrammerMapper;
import com.chixing.ih.module.programmer.pojo.Attention;
import com.chixing.ih.module.programmer.pojo.Programmer;
import com.chixing.ih.module.programmer.pojo.UserImage;
import com.chixing.ih.module.programmer.pojo.dto.AttentionDTO;
import com.chixing.ih.module.programmer.service.IAttentionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @version 1.0
 * @Author MasterYan
 * @Date 2020/10/4
 * Time: 11:04
 */
@Service
@Transactional
public class AttentionServiceImpl implements IAttentionService {

    @Autowired
    AttentionMapper attentionMapper;

    @Autowired
    ProgrammerMapper programmerMapper;

    @Autowired
    ImageMapper imageMapper;

    @Override
    public List<AttentionDTO> listFollower(long followerId) {
        List<AttentionDTO> attentions = attentionMapper.listFollower(followerId);
        for (AttentionDTO attentionDTO : attentions
        ) {
            Programmer programmerMapperOne = programmerMapper.getOne(attentionDTO.getAttention().getFollowedId());
            attentionDTO.setProgrammer(programmerMapperOne);
            UserImage oneByUserId = imageMapper.getOneByUserId(attentionDTO.getAttention().getFollowedId());
            attentionDTO.setImageUrl(oneByUserId.getImageUrl());
        }
        return attentions;
    }

    @Override
    public List<AttentionDTO> listFan(long followedId) {
        List<AttentionDTO> attentions = attentionMapper.listFan(followedId);
        for (AttentionDTO attentionDTO : attentions
        ) {
            Programmer programmerMapperOne = programmerMapper.getOne(attentionDTO.getAttention().getFollowerId());
            attentionDTO.setProgrammer(programmerMapperOne);
            UserImage oneByUserId = imageMapper.getOneByUserId(attentionDTO.getAttention().getFollowerId());
            attentionDTO.setImageUrl(oneByUserId.getImageUrl());
        }
        return attentions;
    }

    @Override
    public boolean saveAttention(Attention attention) {
        int record = attentionMapper.insertOne(attention);
        if (record > 0)
            return true;
        else
            return false;
    }

    @Override
    public boolean deleteAttention(long followerId, long followedId) {
        int record = attentionMapper.deleteOne(followerId, followedId);
        if (record > 0)
            return true;
        else
            return false;
    }
}
